/*
 *  Copyright 2011 Arngrimur Bjarnason 
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 

/**
 * 
 */
package org.arngrimur.deviationtable.activity;

import org.arngrimur.deviationtable.R;
import org.arngrimur.deviationtable.R.string;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;

/**
 * @author Arngrimur Bjarnason, arngrimurbjarnason@gmail.com
 * 
 */
public class AddCompassDegree extends AddItem {
	public static final String COMPASS_NAME_KEY = "compass-name";
	public static final String NEW_COMPASS_DEGREE = "new-compass-degree";
	public static final int REQUEST_CODE = 0xF2;
	public static final String REFERENCE_VALUE = "referenceValue";
	private String compassName;
	private String referenceValue;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		compassName = intent.getStringExtra(COMPASS_NAME_KEY);
		this.referenceValue = intent.getStringExtra(REFERENCE_VALUE);
		setTitle(getString(R.string.add_degree_for_) + compassName);
		textInput.setHint(R.string.compass_degree);
		textInput.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL );
		sendButton.setText(R.string.addDegreeButtonText);

		sendButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent result = new Intent();
				
				Double value = null;
				try {
				value  = new Double(textInput.getText().toString());
				} catch (NumberFormatException e) {
					value = Double.NaN;
				}
				
				result.putExtra(NEW_COMPASS_DEGREE, value);
				result.putExtra(COMPASS_NAME_KEY, compassName);
				result.putExtra(REFERENCE_VALUE, referenceValue);
				setResult(Activity.RESULT_OK, result);
				finish();
			}
		});

	}

}
